#include <bits/stdc++.h>
#define int long long
using namespace std;

int T, n, a[200005], tree[4000005], tag[4000005];

void build(int rt, int l, int r)
{
	tag[rt] = 0;
	if (l == r) tree[rt] = -114514191981000;
	else
	{
		int mid = (l + r) / 2;
		build(rt * 2, l, mid);
		build(rt * 2 + 1, mid + 1, r);
		tree[rt] = max(tree[rt * 2], tree[rt * 2 + 1]);
	}
}

void update1(int rt, int l, int r, int tl, int tr, int x)
{
	if (tl <= l && r <= tr)
	{
		tree[rt] += x;
		tag[rt] += x;
	}
	else
	{
		int mid = (l + r) / 2;
		if (tag[rt])
		{
			tree[rt * 2] += tag[rt];
			tag[rt * 2] += tag[rt];
			tree[rt * 2 + 1] += tag[rt];
			tag[rt * 2 + 1] += tag[rt];
			tag[rt] = 0;
		}
		if (tl <= mid) update1(rt * 2, l, mid, tl, tr, x);
		if (tr > mid) update1(rt * 2 + 1, mid + 1, r, tl, tr, x);
		tree[rt] = max(tree[rt * 2], tree[rt * 2 + 1]);
	}
}

void update2(int rt, int l, int r, int t, int x)
{
	if (l == r)	tree[rt] = max(tree[rt], x);
	else
	{
		int mid = (l + r) / 2;
		if (tag[rt])
		{
			tree[rt * 2] += tag[rt];
			tag[rt * 2] += tag[rt];
			tree[rt * 2 + 1] += tag[rt];
			tag[rt * 2 + 1] += tag[rt];
			tag[rt] = 0;
		}
		if (t <= mid) update2(rt * 2, l, mid, t, x);
		else update2(rt * 2 + 1, mid + 1, r, t, x);
		tree[rt] = max(tree[rt * 2], tree[rt * 2 + 1]);
	}
}

int query(int rt, int l, int r, int tl, int tr)
{
	if (tl <= l && r <= tr) return tree[rt];
	int mid = (l + r) / 2;
	if (tag[rt])
	{
		tree[rt * 2] += tag[rt];
		tag[rt * 2] += tag[rt];
		tree[rt * 2 + 1] += tag[rt];
		tag[rt * 2 + 1] += tag[rt];
		tag[rt] = 0;
	}
	int maxn = -114514191981000;
	if (tl <= mid) maxn = max(maxn, query(rt * 2, l, mid, tl, tr));
	if (tr > mid) maxn = max(maxn, query(rt * 2 + 1, mid + 1, r, tl, tr));
	return maxn;
}

int gx(int x, int y)
{
	if (x == y) return x;
	return 0;
}

signed main()
{
	freopen("color.in", "r", stdin);
	freopen("color.out", "w", stdout);
	scanf("%lld", &T);
	while (T--)
	{
		scanf("%lld", &n);
		for (int i = 1; i <= n; i++)
			scanf("%lld", &a[i]);
		a[0] = 1000001;
		build(1, 1, 1000001);
		update2(1, 1, 1000001, 1000001, 0);
		for (int i = 1; i <= n; i++)
		{
			int x = query(1, 1, 1000001, 1, 1000001);
			x = max(x, query(1, 1, 1000001, a[i], a[i]) + a[i]);
			update1(1, 1, 1000001, 1, 1000001, gx(a[i - 1], a[i]));
			update2(1, 1, 1000001, a[i - 1], x);
		}
		printf("%lld\n", query(1, 1, 1000001, 1, 1000000));
	}
	return 0;
}
